﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Info
{
    public partial class EditMagazine : PageBase
    {
        string act = string.Empty;
        string id = string.Empty;
        BLL.Magazine bll = new BLL.Magazine();
        Model.Magazine model = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("Content", "modify");

            act = Request.QueryString["act"];
            id = Request.QueryString["id"];

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(id))
                {
                    GetData();
                }
            }

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (act == "add")
            {
                model = new Model.Magazine();
                model.Year = txtYear.Text;
                model.Period = txtPeriod.Text;
                model.Unit1 = txtUnit1.Text;
                model.Unit2 = txtUnit2.Text;
                model.Chief = txtChief.Text;
                model.ISSN = txtISSN.Text;
                model.CNISSN = txtCnISSN.Text;
                model.MailCode = txtMailCode.Text;
                model.Detail = FCKeditor1.Value;
                model.Chapter = FCKeditor2.Value;
                model.Summary = FCKeditor3.Value;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadpic/";
                img.FormFile = File1;
                img.Upload();
                if (img.Error == 0)
                {
                    model.Pic = img.OutFileName;
                }
                model.ChannelId = int.Parse(ddlType.SelectedValue);
                model.Hits = 0;
                model.IsTop = cbTop.Checked == true ? 1 : 0;
                bll.Add(model);

                MessageBox.ShowAndRedirect(this.Page, "添加成功", "Magazine.aspx");
            }
            if (act == "edit")
            {
                model = bll.GetModel(int.Parse(id));
                model.Year = txtYear.Text;
                model.Period = txtPeriod.Text;
                model.Unit1 = txtUnit1.Text;
                model.Unit2 = txtUnit2.Text;
                model.Chief = txtChief.Text;
                model.ISSN = txtISSN.Text;
                model.CNISSN = txtCnISSN.Text;
                model.MailCode = txtMailCode.Text;
                model.Detail = FCKeditor1.Value;
                model.Chapter = FCKeditor2.Value;
                model.Summary = FCKeditor3.Value;
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadpic/";
                img.FormFile = File1;
                img.Upload();
                if (img.Error == 0)
                {
                    model.Pic = img.OutFileName;
                }
                model.ChannelId = int.Parse(ddlType.SelectedValue);
                model.IsTop = cbTop.Checked == true ? 1 : 0;
                bll.Update(model);
                MessageBox.ShowAndRedirect(this.Page, "修改成功", "Magazine.aspx");
            }
        }

        private void GetData()
        {
            model = bll.GetModel(int.Parse(id));
            txtYear.Text = model.Year;
            txtPeriod.Text = model.Period;
            txtUnit1.Text = model.Unit1;
            txtUnit2.Text = model.Unit2;
            txtChief.Text = model.Chief;
            txtISSN.Text = model.ISSN;
            txtCnISSN.Text = model.CNISSN;
            txtMailCode.Text = model.MailCode;
            FCKeditor1.Value = model.Detail;
            FCKeditor2.Value = model.Chapter;
            FCKeditor3.Value = model.Summary;
            if (!string.IsNullOrEmpty(model.Pic))
            {
                Image1.ImageUrl = "~/uploadpic/" + model.Pic;
            }
            ddlType.SelectedValue = model.ChannelId.ToString();
            cbTop.Checked = model.IsTop == 1 ? true : false;
        }
    }
}